<?php
/**
 * wrapper class for pagetemplate table
 * @package naanal
 */
class ClsWVirtualfield extends ClsWrapper
{
	//protected $id=null;
    /**
     * name
     * @package naanal
     * @var string 
     */
	protected $name=null;  
        /**
         * template
         * @package naanal
         * @var string 
         */
	protected $template=null;  
        /**
         * pdo
         * @package naanal
         * @var string 
         */
	private $pdo=null;
	  
	//public $tableName=null;
    
	/**
         * construct
         * @package naanal
         */
        function __construct()
	{                            
            parent::__construct();
	}
    /**
     * get id
     * @package naanal 
     * @return integer
     */
	function get_id()
	{
		return $this->id;
	}
        /**
         * set id
         * @package integer
         * @param integer $id
         */
	function set_id($id)
	{
		$this->id=$id;
	}
        /**
         * get name
         * @package naanal
         * @return string
         */
	function get_name()
	{
		return $this->name;
	}
        /**
         * set name
         * @package naanal
         * @param string $name
         */
	function set_name($name)
	{
		$this->name=$name;
	}
	/**
         * get template
         * @package naanal
         */
	function get_template()
	{
		$tpl = str_replace("\\r\\n","",$this->template);
	}
        /**
         * set template
         * @package naanal
         * @param string $template
         */
	function set_template($template)
	{                       
		$this->template=str_replace("\\r\\n","",$template); 
	}
	/**
         * load
         * @package naanal
         * @return boolean
         */
	public function load($id=0)
  	{
  		$sql="SELECT * FROM `$this->tableName` WHERE `id`='$this->id'";
  		$this->pdo->setQuery($sql);
  		$arrRow=$this->pdo->getAllAssoc();
  		if(empty($arrRow)) return false;
  		$this->setArray($arrRow[0]);
  	}
  	/**
         * insert
         * @package naanal
         * @param string $sql
         */
  	public function insert($sql=null)
  	{
  		if(is_null($sql))
  		{
  			$sql="INSERT INTO `$this->tableName` (`name`,`data`,`sort`) values(:name,:data,:sort)";
  		}
  		$this->pdo->setQuery($sql,$this->getArray());
  	}
  	/**
         * update
         * @package naanal
         * @param string $sql
         */
	public function update($sql=null)
  	{
  		if(empty($this->id)) trace("This object has empty ID");
  		if(is_null($sql))
  		{
  			$sql="UPDATE `$this->tableName` SET `name`=:name,`data`=:data,`sort`=:sort  WHERE `id`=:id";
  		}
  		$this->pdo->setQuery($sql,$this->getArray());
  	}
  	/**
         * delete
         * @package naanal
         */
  	public function delete()
  	{
  		if(empty($this->id)) trace("This object has empty ID");
  		$sql="DELETE FROM `{$this->tableName}` WHERE `id`=".$this->id;
  		$this->pdo->setQuery($sql);
  		$this->id=null;
  	} 
        /**
         * getArray
         * @package naanal
         * @return array
         */
	function getArray()
	{
		$arr=null;
		$arr["id"]=$this->id;
		$arr["name"]=$this->name;
		$arr["data"]=$this->data;	
                $arr["sort"]=$this->sort;
		return $arr;
	}
        /**
         * set Array
         * @package naanal
         * @param array $arr
         */
	function setArray($arr)
	{
		if(isset($arr["id"])) $this->set_id($arr["id"]);
		if(isset($arr["name"])) $this->set_name($arr["name"]);
		if(isset($arr["data"])) $this->set_template($arr["data"]);
                if(isset($arr["sort"])) $this->set_template($arr["sort"]);
	}       
}
?>